package com.zcc.filter;

import javax.servlet.*;
import java.io.IOException;

//继承Filter，导入Servlet包下的
public class CharacterEncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //编写过滤器规则
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html;charset=UTF-8");

        System.out.println("过滤器执行前");
        //让请求被过滤后继续执行
        filterChain.doFilter(servletRequest, servletResponse);

        System.out.println("过滤器执行后");
    }

    //初始化，在服务器部署的时候启动
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter 初始化");
    }

    //销毁，在服务器关闭的时候启动
    @Override
    public void destroy() {
        System.out.println("CharacterEncodingFilter 销毁");
    }
}
